package com.gangwangchina.android.loveynotify;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import com.kismobile.framework.mcf.task.KAsyncTask;
import com.kismobile.framework.mcf.task.TaskListener;
import com.kismobile.framework.mcf.task.TaskParams;
import com.kismobile.framework.mcf.task.TaskResult;
import com.kismobile.framework.mcf.util.date.KDate;

import android.R.bool;
import android.R.integer;
import android.content.ContentValues;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

public class Service extends android.app.Service implements ICountService {

	private cache taskCache = null;
	public static final SimpleDateFormat dateFormat = new SimpleDateFormat(
	"HH:mm:ss"); 
	
	private TaskListener mCheckNotifyTaskListener = new TaskListener() {

		@Override
		public void onProgressUpdate(KAsyncTask task, Object param) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPreExecute(KAsyncTask task) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPostExecute(KAsyncTask task, TaskResult result) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onCancelled(KAsyncTask task) {
			// TODO Auto-generated method stub

		}

		@Override
		public String getName() {
			// TODO Auto-generated method stub
			return null;
		}
	};

	private KAsyncTask mCheckNotifyTask = new KAsyncTask() {

		@Override
		protected TaskResult _doInBackground(TaskParams... params) {
			// TODO Auto-generated method stub
			if(taskCache == null)
			{
				taskCache = new NotifyTask(getApplicationContext());			
			}
			taskCache.init();
			
			List<Integer> idList = taskCache.getIds();
			
			
			while (true) {
				
				for(Integer _id : idList)
				{
					long time = taskCache.queryLong(NotifyTask.COLUMN_NOTIFY_TIME, _id);						
					KDate notifyTime = new KDate();
					notifyTime.setTime(time);
					
					KDate nowTime = new KDate();
					
					
					if(notifyTime.getHours() == nowTime.getHours() &&
							notifyTime.getMinutes() == nowTime.getMinutes())
					
					{
						
						String type = taskCache.queryString(NotifyTask.COLUMN_NOTIFY_TYPE_, _id);
						
						Intent dialogIntent = new Intent(getBaseContext(),
								LoveyNotifyActivity.class);
						dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						
						dialogIntent.putExtra(LoveyNotifyActivity._TYPE_ACTIVITY_KEY_, type);
//						if(type.equals(LoveyNotifyActivity._TYPE_NOTIFY_TIMEUP_))
//						{
//							dialogIntent.putExtra(LoveyNotifyActivity._TYPE_ACTIVITY_KEY_, type);
//						}
//						else if(type.equals(LoveyNotifyActivity._TYPE_NOTIFY_HEART_))
//						{
//							dialogIntent.putExtra(LoveyNotifyActivity._TYPE_ACTIVITY_KEY_, type);
//						}
						
						
						getApplication().startActivity(dialogIntent);
						
						try {
							Thread.sleep(60 * 1000);
						} catch (Exception e) {
							// TODO: handle exception
						}

					}
					
				}				
			}
		}
	};

	private int count;

	private ServiceBinder serviceBinder = new ServiceBinder();

	public class ServiceBinder extends Binder implements ICountService {
		@Override
		public int getCount() {
			return count;
		}
	}

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return serviceBinder;
	}

	@Override
	public void onCreate() {
		super.onCreate();

		InitDataBaseIfNot();
		
		
		if (mCheckNotifyTask == null) {
			return;
		}

		if (mCheckNotifyTask.getStatus() != KAsyncTask.Status.RUNNING || 
				mCheckNotifyTask.getStatus() != KAsyncTask.Status.PENDING) {

			mCheckNotifyTask.setListener(mCheckNotifyTaskListener);
			TaskParams params = new TaskParams();

			mCheckNotifyTask.execute(params);
		}
	}

	public int getCount() {
		return count;
	}

	
	private boolean isInitDataBase()
	{
		if(taskCache == null)
		{
			taskCache = new NotifyTask(getApplicationContext());			
		}
		taskCache.init();
				
		return taskCache.getCount() != 0 ? true : false;
	}
	
	private void InitDataBaseIfNot()
	{
		if(isInitDataBase())
			return;
		
		if(taskCache == null)
		{
			taskCache = new NotifyTask(getApplicationContext());			
		}
		taskCache.init();
		
		ContentValues values = new ContentValues();
		values.put(NotifyTask.COLUMN_NOTIFY_TYPE_, LoveyNotifyActivity._TYPE_NOTIFY_TIMEUP_);
		try {
			values.put(NotifyTask.COLUMN_NOTIFY_TIME, KDate.DATEFORMAT.parse("2000-01-01 17:29:00").getTime());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		taskCache.insert(values);
		
		
		values.clear();
		values.put(NotifyTask.COLUMN_NOTIFY_TYPE_,
				LoveyNotifyActivity._TYPE_NOTIFY_TIMEUP_);
		try {
			values.put(NotifyTask.COLUMN_NOTIFY_TIME, KDate.DATEFORMAT.parse("2000-01-01 12:29:00").getTime());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		taskCache.insert(values);
		
		
		values.clear();
		values.put(NotifyTask.COLUMN_NOTIFY_TYPE_,
				LoveyNotifyActivity._TYPE_NOTIFY_WORK_);
		try {
			values.put(NotifyTask.COLUMN_NOTIFY_TIME, KDate.DATEFORMAT.parse("2000-01-01 14:10:00").getTime());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		taskCache.insert(values);
		
		
		values.clear();
		values.put(NotifyTask.COLUMN_NOTIFY_TYPE_,
				LoveyNotifyActivity._TYPE_NOTIFY_HEART_);
		try {
			values.put(NotifyTask.COLUMN_NOTIFY_TIME, KDate.DATEFORMAT.parse("2000-01-01 09:03:00").getTime());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		taskCache.insert(values);
		
		
		values.clear();
		values.put(NotifyTask.COLUMN_NOTIFY_TYPE_,
				LoveyNotifyActivity._TYPE_NOTIFY_HEART_);
		try {
			values.put(NotifyTask.COLUMN_NOTIFY_TIME, KDate.DATEFORMAT.parse("2000-01-01 13:35:00").getTime());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		taskCache.insert(values);
		
		
		values.clear();
		values.put(NotifyTask.COLUMN_NOTIFY_TYPE_,
				LoveyNotifyActivity._TYPE_NOTIFY_JQ_);
		try {
			values.put(NotifyTask.COLUMN_NOTIFY_TIME, KDate.DATEFORMAT.parse("2000-01-01 10:20:00").getTime());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		taskCache.insert(values);
		
		
		values.clear();
		values.put(NotifyTask.COLUMN_NOTIFY_TYPE_,
				LoveyNotifyActivity._TYPE_NOTIFY_JQ_);
		try {
			values.put(NotifyTask.COLUMN_NOTIFY_TIME, KDate.DATEFORMAT.parse("2000-01-01 22:20:00").getTime());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		taskCache.insert(values);
		
		values.clear();
		values.put(NotifyTask.COLUMN_NOTIFY_TYPE_,
				LoveyNotifyActivity._TYPE_NOTIFY_WORK_);
		try {
			values.put(NotifyTask.COLUMN_NOTIFY_TIME, KDate.DATEFORMAT.parse("2000-01-01 11:10:00").getTime());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		taskCache.insert(values);
		
		values.clear();
		values.put(NotifyTask.COLUMN_NOTIFY_TYPE_,
				LoveyNotifyActivity._TYPE_NOTIFY_JQ_);
		try {
			values.put(NotifyTask.COLUMN_NOTIFY_TIME, KDate.DATEFORMAT.parse("2000-01-01 23:00:00").getTime());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		taskCache.insert(values);
		
		
		values.clear();
		values.put(NotifyTask.COLUMN_NOTIFY_TYPE_,
				LoveyNotifyActivity._TYPE_NOTIFY_WORK_);
		try {
			values.put(NotifyTask.COLUMN_NOTIFY_TIME, KDate.DATEFORMAT.parse("2000-01-01 21:10:00").getTime());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		taskCache.insert(values);
		
//		for(int i = 0; i < 10; i++)
//		{
//			values.clear();
//			values.put(NotifyTask.COLUMN_NOTIFY_TYPE_,
//					LoveyNotifyActivity._TYPE_NOTIFY_HEART_);
//			try {
//				values.put(NotifyTask.COLUMN_NOTIFY_TIME, KDate.DATEFORMAT.parse(String.format("2000-01-01 17:%d:00", i + 40)).getTime());
//			} catch (ParseException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//			taskCache.insert(values);
//		}
	}
	
	
	private void InitDataBaseForce()
	{
		if(taskCache == null)
		{
			taskCache = new NotifyTask(getApplicationContext());			
		}
		taskCache.init();
		taskCache.deleteAll();
		
		InitDataBaseIfNot();
	}
	
}
